package com.base.power.demo.sys.controller;

import com.base.power.demo.common.base.constant.SystemStaticConst;
import com.base.power.demo.common.base.controller.GenericController;
import com.base.power.demo.common.base.entity.Page;
import com.base.power.demo.common.base.service.GenericService;
import com.base.power.demo.sys.entity.QueryTree;
import com.base.power.demo.sys.entity.Tree;
import com.base.power.demo.sys.entity.UserRole;
import com.base.power.demo.sys.mapper.tree.TreeMapper;
import com.base.power.demo.sys.service.RoleService;
import com.base.power.demo.sys.service.TreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述：菜单controller类
 */
@Controller
@RequestMapping("/tree")
public class TreeController extends GenericController<Tree,QueryTree> {

    @Autowired
    private TreeService treeService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private TreeMapper treeMapper;

    @Override
    protected GenericService<Tree, QueryTree> getService() {
        return treeService;
    }

    /**
     * 功能描述：跳转到修改菜单按钮节点的页面
     * @param entity
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/updateTreeButtonPage")
    public String updateTreeButtonPage(Tree entity,Model model) throws Exception{
        entity = treeService.get(entity);
        Tree pTree = treeService.get(new Tree(entity.getpId()));
        entity.setTree(pTree);
        model.addAttribute("entity",entity);
        return getPageBaseRoot()+"/updateButton";
    }

    /**
     * 功能描述：跳转到增加菜单按钮节点的页面
     * @param entity
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/addTreeButtonPage")
    public String addTreeButtonPage(Tree entity,Model model) throws Exception{
        entity = treeService.get(entity);
        model.addAttribute("entity",entity);
        return getPageBaseRoot()+"/addButton";
    }

    /**
     * 功能描述：跳转到修改菜单节点的页面
     * @param entity
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/updateTreePage")
    public String updateTreePage(Tree entity,Model model) throws Exception{
        entity = treeService.get(entity);
        Tree pTree = null;
        if(entity.getpId()==0l){
            pTree = new Tree();
            pTree.setId(0);
            pTree.setName("顶层节点");
        }else{
            pTree = treeService.get(new Tree(entity.getpId()));
        }
        entity.setTree(pTree);
        model.addAttribute("entity",entity);
        return getPageBaseRoot()+UPDATEPAGE;
    }

    /**
     * 功能描述：跳转到增加菜单节点的页面
     * @param entity
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/addTreePage")
    public String addPage(Tree entity,Model model) throws Exception{
        if(entity.getId()==0){
            entity = new Tree();
            entity.setId(0);
            entity.setName("顶层节点");
        }else{
            entity = treeService.get(entity);
        }
        model.addAttribute("entity",entity);
        return getPageBaseRoot()+ADDPAGE;
    }

    /**
     * 功能描述：获取菜单底下的按钮节点数据
     * @param queryTree
     * @return
     */
    @RequestMapping(value="/treeButtonList",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> treeButtonList(QueryTree queryTree){
        Map<String,Object> result = new HashMap<String, Object>();
        Page page = treeService.findByPage(queryTree);
        result.put("totalCount",page.getTotal());
        result.put("result",page.getRows());
        return result;
    }

    /**
     * 功能描述：直接加载整个菜单树的数据
     * @return
     */
    @RequestMapping(value = "/loadTree",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> loadTree(QueryTree queryTree){
        Map<String,Object> result = new HashMap<String, Object>();
        List<Tree> treeList = treeService.query(queryTree);
        result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
        result.put(SystemStaticConst.MSG,"加载菜单数据成功！");
        result.put("data",treeMapper.treesToTressDTOs(treeList));
        return result;
    }


    /**
     * 功能描述：根据角色类型来加载菜单树
     * @param entity
     * @return
     */
    @RequestMapping(value = "/loadUserTree",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Map<String,Object> loadUserTree(UserRole entity){
        Map<String,Object> result = new HashMap<String, Object>();
        List<Tree> treeList = treeService.query(null);
        if(entity.getId()!=0){
            entity = roleService.getRoleRelationTree(entity);
            if(entity!=null){
                for(Tree tree:entity.getTreeList()){
                    treeList.stream().forEach(t->{
                        if(t.getId()==tree.getId()){
                            t.setChecked(true);
                        }
                    });
                }
            }
        }
        result.put(SystemStaticConst.RESULT, SystemStaticConst.SUCCESS);
        result.put(SystemStaticConst.MSG,"加载菜单数据成功！");
        result.put("data",treeList);
        return result;
    }
}
